package com.growthsee.rider.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.ImmutableMap;
import com.growthsee.common.core.domain.entity.SysDictData;
import com.growthsee.common.core.domain.entity.SysUser;
import com.growthsee.rider.domain.RiderCompany;
import com.growthsee.rider.domain.RiderInfo;
import com.growthsee.rider.domain.dto.RiderCompanyDto;
import com.growthsee.rider.enumeration.YesNoEnum;
import com.growthsee.rider.mapper.RiderCompanyMapper;
import com.growthsee.rider.mapper.RiderInfoMapper;
import com.growthsee.rider.utils.ApplicationContexts;
import com.growthsee.system.mapper.SysDictDataMapper;
import com.growthsee.system.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ycj
 * @Description: 评价活动视图包装类
 * @date 2020/05/24
 **/
public class CompanyWrapper {


    public static CompanyWrapper builder() {
        return new CompanyWrapper();
    }



    public String check(RiderCompany company){
        RiderCompanyMapper riderQuestionTypeMapper = ApplicationContexts.getBean(RiderCompanyMapper.class);
        LambdaQueryWrapper<RiderCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RiderCompany::getDeleted, YesNoEnum.NO.getValue());
        if(StringUtils.isNotEmpty(company.getName())){
            queryWrapper.eq(RiderCompany::getName,company.getName());
        }
        if(StringUtils.isNotEmpty(company.getId())){
            queryWrapper.ne(RiderCompany::getId,company.getId());
        }
        Integer integer = riderQuestionTypeMapper.selectCount(queryWrapper);
        if(integer>0){
            return "公司名已存在，请重新输入";
        }else return "";
    }

    /**
     * 公司数据处理
     * @param records
     */
    public void dataHandler(List<RiderCompanyDto> records) {
        if(CollectionUtils.isNotEmpty(records)){
            List<String>ids = records.stream().map(s->s.getId()).distinct().collect(Collectors.toList());
            String headIds = records.stream().filter(v -> ObjectUtils.isNotEmpty(v.getHeadId())).map(s -> s.getHeadId().toString()).distinct().collect(Collectors.joining(","));
            RiderInfoMapper riderInfoMapper = ApplicationContexts.getBean(RiderInfoMapper.class);
            List<RiderInfo>riderInfos =riderInfoMapper.selectList(Wrappers.<RiderInfo>lambdaQuery().in(RiderInfo::getPlatformCompanyId,ids).or().in(RiderInfo::getAgentCompanyId,ids));
            SysDictDataMapper sysDictDataMapper = ApplicationContexts.getBean(SysDictDataMapper.class);
            List<SysDictData> types = sysDictDataMapper.selectDictDataByType("gslx");//公司类型
            SysUserMapper sysUserMapper = ApplicationContexts.getBean(SysUserMapper.class);
            List<SysUser> heads =sysUserMapper.getCompanyHead(ImmutableMap.of("ids",headIds,"exclude","0"));
            for (RiderCompanyDto companyDto : records) {
                List<RiderInfo>riderInfoList = riderInfos.stream().filter(s->StringUtils.equals(s.getPlatformCompanyId(),companyDto.getId()) || StringUtils.equals(s.getAgentCompanyId(),companyDto.getId())).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(riderInfoList)){
                    companyDto.setRiderNum(riderInfoList.size());
                    double sum = riderInfoList.stream().mapToDouble(s -> s.getCumulativeScore().doubleValue()).sum();
                    companyDto.setCumulativeScore(new BigDecimal(sum));
                }
                if(CollectionUtils.isNotEmpty(types)){
                    SysDictData sysDictData = types.stream().filter(s -> StringUtils.equals(s.getDictValue(), companyDto.getType())).findFirst().orElse(null);
                    if(ObjectUtils.isNotEmpty(sysDictData)){
                        companyDto.setTypeName(sysDictData.getDictLabel());
                    }
                }
                if(CollectionUtils.isNotEmpty(heads)){
                    SysUser sysUser = heads.stream().filter(s -> s.getUserId() == companyDto.getHeadId()).findFirst().orElse(null);
                    if(ObjectUtils.isNotEmpty(sysUser)){
                        companyDto.setHeader(sysUser);
                    }
                }
            }
        }

    }
}
